package inet.app.tuvi.dialog;

import inet.app.tuvi.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyAlertDialogFragment extends DialogFragment
	implements OnClickListener {
	
	Context mContext;
	LayoutInflater mInflater;
	TextView mTitleView;
	TextView mContentView;
	Button mPositiveBtn;
	Button mNegativeBtn;
	
	String mTitle;
	String mContent;
	String mPositiveLabelBtn;
	String mNegativeLabelBtn;
	PositiveListener mPositiveListener;
	boolean isHidePosBtn;
	boolean isHideNegBtn;
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.negative_btn:
			this.dismissAllowingStateLoss();
			break;
		case R.id.positive_btn:
			if(null != mPositiveListener) {
				mPositiveListener.onPositive();
			}
		default:
			break;
		}
	}
	
	/**
	 * 
	 * @param context
	 * @param title	   title of dialog
	 * @param content  content of dialog
	 * @param posLabel Label of positive button
	 * @param nevLabel Label of negative button
	 * @param listener onClick of positive button
	 */
	public MyAlertDialogFragment(Context context, String title
			, String content, String posLabel, String nevLabel, PositiveListener listener) {
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
		this.mTitle = title;
		this.mContent = content;
		this.mPositiveLabelBtn = posLabel;
		this.mNegativeLabelBtn = nevLabel;
		this.mPositiveListener = listener;
		
	}
	
	public static MyAlertDialogFragment newInstance(Context context, String title
			, String content, String posLabel, String nevLabel, PositiveListener listener) {
		return new MyAlertDialogFragment(context, title, content, posLabel, nevLabel, listener);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		Dialog dialog = new Dialog(mContext, android.R.style.Theme_Dialog);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setCancelable(true);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		View view = mInflater.inflate(R.layout.dialog_alert, null);
		mTitleView = (TextView) view.findViewById(R.id.title_txtview);
		mTitleView.setText(mTitle);
		mContentView = (TextView) view.findViewById(R.id.content_txtview);
		mContentView.setText(mContent);
		mPositiveBtn = (Button) view.findViewById(R.id.positive_btn);
		mPositiveBtn.setText(mPositiveLabelBtn);
		mPositiveBtn.setOnClickListener(this);
		mNegativeBtn = (Button) view.findViewById(R.id.negative_btn);
		mNegativeBtn.setText(mNegativeLabelBtn);
		mNegativeBtn.setOnClickListener(this);
		
		//hide negative button and show positive button at center horizontal
		if(isHideNegBtn) {
			mNegativeBtn.setVisibility(View.GONE);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mPositiveBtn.getLayoutParams();
			params.addRule(RelativeLayout.CENTER_HORIZONTAL);
			mPositiveBtn.setLayoutParams(params);
		}
		
		//hide positive button and show negative button at center horizontal
		if(isHidePosBtn) {
			mPositiveBtn.setVisibility(View.GONE);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mNegativeBtn.getLayoutParams();
			params.addRule(RelativeLayout.CENTER_HORIZONTAL);
			mNegativeBtn.setLayoutParams(params);
		}
		
		dialog.setContentView(view);
		return dialog;
	}
	
	public interface PositiveListener {
		public void onPositive();
	}
	
	public void hidePositiveButton(boolean isHide) {
		isHidePosBtn = isHide;
	}
	
	public void hideNegativeButton(boolean isHide) {
		isHideNegBtn = isHide;
	}
}
